package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Article;
import com.hs.sfzy.entity.Company;
import com.hs.sfzy.service.impl.CompanyServiceImpl;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName PublicityController
 * @Description TODO
 * @Author liubo
 * @Date 2024年10月23日 18:56
 * @Version 1.0
 */
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyServiceImpl companyService;

    @PostMapping("/add")
    public Result insert(@RequestBody Company company){
        int num = companyService.insert(company);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        int num = companyService.delete(id);
        if (num != 0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @PutMapping("/edit")
    public Result update(@RequestBody Company company) {
        int num = companyService.update(company);
        if (num != 0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }


    @GetMapping
    public Result selectAll(@RequestBody Company company) {
        List<Company> companies = companyService.selectAll(company);
        return Result.success(companies);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id) {
        Company company = companyService.selectById(id);
        return Result.success(company);
    }

    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            Company company) {
        PageInfo<Company> info = companyService.selectByPageInfo(pageNum, pageSize, company);
        return Result.success(info);
    }



}
